const AccountService = require('../services/accountService');
let R = require('../result/r');

/**
 * accountController
 * Controller 是业务入口，由 HTTP 路由解析后调用
 * 包含记账事项的增删改查功能
 */
class accountController {
    /**
     * 列出所有记账事项
     */
    async listAll(acc) {
        const list = await AccountService.listAll();
        acc.body = R.ok().data(list);
    }

    /**
     * 查询分页
     */
    async queryById(acc){
        const params = acc.request.body;
        const list = await AccountService.queryById(params);
        acc.body = R.ok().data(list);
    }

    /**
     * 查询一条数据根据id
     */
    async getOneById(acc) {
        const one = await AccountService.getOneById(acc.params.id);
        acc.body = R.ok().data(one);
    }

    /**
     * 创建一条记账事项
     */
    async add(acc) {
        const Account = acc.request.body;
        const result = await AccountService.create(Account);
        acc.body = R.ok("新增成功").data(result);
    }

    /**
     * 删除一条记账事项
     * @param acc Koa 的上下文参数
     */
    async deleteById(acc) {
        //console.log(acc.params.id)
        await AccountService.delete(acc.params.id);
        acc.body = R.ok("删除成功");
    }


    /**
     * 更新记账事项
     */
    async updateOneById(acc) {
        const Account = acc.request.body;
        await AccountService.update(Account);
        acc.body = R.ok("更新成功");
    }

    /**
     * 分组类目收入
     */
    async getGroupTypeIncomePrice(ctx) {
        let infoId = ctx.params.info_id;
        let res = await AccountService.getGroupTypeIncomePrice(infoId);
        ctx.body = R.ok().data(res)
    }
    /**
     * 分组支付类型支出
     */
    async getGroupPaymentIncomePrice(ctx) {
        let infoId = ctx.params.info_id;
        let res = await AccountService.getGroupPaymentIncomePrice(infoId);
        ctx.body = R.ok().data(res)
    }
    /**
     * 分组类目支出
     */
    async getGroupTypeExpenditurePrice(ctx) {
        let infoId = ctx.params.info_id;
        let res = await AccountService.getGroupTypeExpenditurePrice(infoId);
        ctx.body = R.ok().data(res)
    }

    /**
     * 分组支付类型支出
     */
    async getGroupPaymentExpenditurePrice(ctx) {
        let infoId = ctx.params.info_id;
        let res = await AccountService.getGroupPaymentExpenditurePrice(infoId);
        ctx.body = R.ok().data(res)
    }

    async getUserPrice(acc) {
        let infoId = acc.params.info_id;
        let res = await AccountService.getUserPrice(infoId);
        acc.body = R.ok().data(res)
    }

}

// 导出 Controller 的实例
module.exports = new accountController();
